package codeRandomThoughts.Test96不同的二叉搜索树;

public class Solution2 {
    public int numTrees(int n) {
        //dp[i]:i个结点组成且节点值从1到i互不相同的二叉搜索树有dp[i]种
        int[] dp = new int[20];
        //初始化
        dp[0] = 1;
        dp[1] = 1;
        dp[2] = 2;

        for (int i = 3; i <= n; i++) {
            for (int j = 0; j <= i - 1; j++) {
                dp[i] += (dp[j] * dp[i - 1 - j]);
            }
        }

        return dp[n];
    }
}
